package io.renren.modules.sys.subject.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.sys.subject.service.QuestionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.subject.entity.QuestionEntity;



/**
 * @author flagdied
 * @email 2481019556@qq.com
 * @date 2020-05-01 15:11:48
 */
@RestController
@RequestMapping("subject/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    public static boolean isInteger(String str) {//是整数么？
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }
    //下边的映射都是先判断有没有最基本的CRUD权限，再判断从前端通过路径值传过来
    // 的科目号，判断有没有该科目号的权限, 权限标识subject:xxxxx

    /**
     * 列表
     */
    @RequestMapping("/list/{subjectNo}")
    @RequiresPermissions( "subject:question:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable(value = "subjectNo", required = false) String str) {
        if (isInteger(str)) {
            Integer subjectNo = Integer.valueOf(str);
            if (subjectNo != 0 && ShiroUtils.getSubject().isPermitted("subject:" + subjectNo)) {
                PageUtils page = questionService.queryPage(params, subjectNo);
                return R.ok().put("page", page);
            }
        }
        return R.error("没有该科目的权限");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}/{subjectNo}")
    @RequiresPermissions("subject:question:info")
    public R info(@PathVariable("id") Integer id, @PathVariable(value = "subjectNo", required = false) String str) {
        if (isInteger(str)) {
            Integer subjectNo = Integer.valueOf(str);
            if (subjectNo != 0 && ShiroUtils.getSubject().isPermitted("subject:" + subjectNo)) {
                QuestionEntity question = questionService.getOne(new QueryWrapper<QuestionEntity>()
                        .eq("subject", subjectNo)
                        .eq("id", id));
                return R.ok().put("question", question);
            }
        }
        return R.error("没有该科目的权限");
    }

    /**
     * 保存
     */
    @RequestMapping("/save/{subjectNo}")
    @RequiresPermissions("subject:question:save")
    public R save(@RequestBody QuestionEntity question, @PathVariable(value = "subjectNo", required = false) String str) {
        if (isInteger(str)) {
            Integer subjectNo = Integer.valueOf(str);
            if (subjectNo != 0 && ShiroUtils.getSubject().isPermitted("subject:" + subjectNo)) {
                question.setSubject(subjectNo);
                questionService.insertSelective(question);
                int id = question.getId();
                return R.ok().put("id", id);
            }
        }
        return R.error("没有该科目的权限");
    }

    /**
     * 修改
     */
    @RequestMapping("/update/{subjectNo}")
    @RequiresPermissions("subject:question:update")
    public R update(@RequestBody QuestionEntity question, @PathVariable(value = "subjectNo", required = false) String str) {
        if (isInteger(str)) {
            Integer subjectNo = Integer.valueOf(str);
            if (subjectNo != 0 && ShiroUtils.getSubject().isPermitted("subject:" + subjectNo)) {
                question.setSubject(subjectNo);
                questionService.updateById(question);
                return R.ok();
            }
        }
        return R.error("没有该科目的权限");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{subjectNo}")
    @RequiresPermissions("subject:question:delete")
    public R delete(@RequestBody Integer[] ids,@PathVariable(value = "subjectNo",required = false) String str){
        if (isInteger(str)) {
            Integer subjectNo = Integer.valueOf(str);
            if (subjectNo != 0 && ShiroUtils.getSubject().isPermitted("subject:" + subjectNo)) {
                questionService.remove(new QueryWrapper<QuestionEntity>()
                        .eq("subject",subjectNo)
                        .in("id",(Object[])ids));
                return R.ok();
            }
        }
        return R.error("没有该科目的权限");
    }

}
